package com.example.shixun.controller;

import com.example.shixun.interceptor.Login;
import com.example.shixun.pojo.Exam;
import com.example.shixun.pojo.ExamUser;
import com.example.shixun.pojo.Result;
import com.example.shixun.service.ExamService;
import com.example.shixun.service.ExamUserService;
import com.example.shixun.utils.GetIdByToken;
import com.example.shixun.utils.GetNowTime;
import com.example.shixun.utils.PageInfoUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    ExamService examService;
    @Autowired
    GetIdByToken getIdByToken;
    @Autowired
    ExamUserService examUserService;
    @ApiOperation("添加考试")
    @Login
    @PostMapping("/save")
    public Result saveExam(@RequestBody Exam exam, @RequestHeader("token") String token){
        exam.setTeacherId(getIdByToken.getIdByToken(token));
        exam.setCreateTime(GetNowTime.getNowTime());
        return Result.success(examService.save(exam));
    }

    @ApiOperation("查找所有考试")
    @GetMapping("/list")
    public Result getExamList(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       @RequestParam(value = "orderBy", defaultValue = "join_num") String orderBy,
                       @RequestParam(value = "order", defaultValue = "desc") String order,
                       Exam exam){
        PageInfoUtil<Exam> pageInfoUtil=new PageInfoUtil<Exam>();
        /*排序*/
        String[] orderByArr = {"join_num","name","create_time","id"};
        String orderByStr = "";
        System.out.println("orderBy:"+orderBy+" order:"+order);
        if (orderBy!=null&&orderBy!="" && Arrays.asList(orderByArr).contains(orderBy.toLowerCase())) {
            orderByStr = String.format("%s %s", orderBy.toLowerCase(), "asc".equalsIgnoreCase(order) ? "asc" : "desc");
        } else {
            // 默认排序
            orderByStr = "join_num asc";
        }
        pageInfoUtil.setPageNum(pageNum);
        pageInfoUtil.setPageSize(pageSize);
        pageInfoUtil.setOrderBy(orderByStr);
        pageInfoUtil.setParams(exam);
        return Result.success(examService.findAll(pageInfoUtil));
    }
    @ApiOperation("根据id查找考试")
    @GetMapping("findById")
    public Result findById(@RequestParam("id") Integer id){
        return Result.success(examService.findById(id));
    }
    @ApiOperation("学生参加考试")
    @GetMapping("/addStudent")
    public Result addStudent(@RequestParam("id")Integer id,
                             @RequestHeader("token")String token){
        ExamUser examUser=new ExamUser();
        examUser.setUserId(getIdByToken.getIdByToken(token));
        examUser.setExamId(id);
        examUser.setCreateTime(GetNowTime.getNowTime());
        if(examUserService.findAll(examUser)==null){
            examUserService.save(examUser);
            return Result.success(examService.addStudent(id));
        }
        return Result.fail("已参加此考试，无需重复参加");
    }
    @ApiOperation("学生退出考试")
    @GetMapping("/subStudent")
    public Result subStudent(@RequestParam("id")Integer id,
                             @RequestHeader("token")String token){
        ExamUser examUser=new ExamUser();
        examUser.setUserId(getIdByToken.getIdByToken(token));
        examUser.setExamId(id);
        if(examUserService.findAll(examUser)!=null){
            examUserService.delete(examUser);
            return Result.success(examService.subStudent(id));
        }
        return Result.fail("未参加此考试，无需退出");
    }
    @ApiOperation("查找自己参加的考试")
    @GetMapping("/myExam")
    public Result myExam(@RequestHeader("token") String token){
        ExamUser examUser=new ExamUser();
        /*根据自己的id获取相关的考试id*/
        examUser.setUserId(getIdByToken.getIdByToken(token));
        List<ExamUser> examUserList=new ArrayList<>();
        List<Exam> examList=new ArrayList<>();
        for(ExamUser examUser1:examUserList){
            /*根据考试的id获取考试详情并保存*/
            Exam temp=examService.findById(examUser1.getExamId());
            if(temp!=null){
                examList.add(temp);
            }
        }
        return Result.success(examList);
    }
}
